package wavenotes;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.google.inject.Singleton;

/**
 * Returns slide images.  Expects either the "slideKey" or the "presentationKey" as parameters.
 * If the presentationKey is provide a "slideIndex" can optionally be provided as well 
 * (it will default to the current slide for the presentation).
 *
 */
@Singleton
public class SlideServlet extends HttpServlet {
	private static final long serialVersionUID = 2060849085319434762L;
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		if (ServletFileUpload.isMultipartContent(req)) {
			try {
				//handleSlideUpload(req, resp);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//getSlideImage(req, resp);
	}
	
//	private void getSlideImage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		PersistenceManager pm = PMF.get().getPersistenceManager();
//		
//		Slide slide = null;
//		
//		if (req.getParameter("slideKey") != null) {
//			//get by slide key
//			slide = pm.getObjectById(Slide.class, req.getParameter("slideKey"));
//		} else if (req.getParameter("presentationKey") != null) {
//			Presentation presentation = pm.getObjectById(Presentation.class, req.getParameter("presentationKey"));
//			int slideIndex = presentation.getCurrentSlideIndex();
//			if (req.getParameter("slideIndex") != null) {
//				slideIndex = Integer.parseInt(req.getParameter("slideIndex"));
//			}
//			
//			if (slideIndex > (presentation.getSlides().size() - 1)) {
//				throw new ServletException("Slide index out of range");
//			}
//			
//			slide = presentation.getSlides().get(slideIndex);
//		}
//		
//		String size = "original";
//		
//		if (req.getParameter("size") != null) {
//			size = req.getParameter("size");
//		}
//		
//		if (slide == null) {
//			throw new ServletException("File not found.");
//		}
//		
//		byte[] imageBytes = null;
//		if (size.equals("small")) {
//			imageBytes = slide.getSmallImage().getBytes();
//		} else if (size.equals("original")) {
//			imageBytes = slide.getOriginalImage().getBytes();
//		}
//		
//		resp.setContentType("image/png");
//		resp.setContentLength(imageBytes.length);
//		
//		resp.getOutputStream().write(imageBytes);
//	}
	
//	private void handleSlideUpload(HttpServletRequest req, HttpServletResponse resp) 
//		throws ServletException, FileUploadException, IOException {
//		
//		ServletFileUpload upload = new ServletFileUpload();
//		FileItemIterator iterator = upload.getItemIterator(req);
//		String presentationKey = null;
//		List<Slide> slides = new ArrayList<Slide>();
//		
//		while (iterator.hasNext()) {
//			FileItemStream item = iterator.next();
//			InputStream stream = item.openStream();
//			
//			if (item.isFormField()) {
//				//presentationKey
//				if (item.getFieldName().equals("presentationKey")) {
//					presentationKey = new BufferedReader(new InputStreamReader(stream)).readLine();
//				}
//			} else {
//				//slide
//				if (!item.getName().equals("")) {
//					slides.add(SlideFactory.createSlide(0, Util.readStream(stream), null));
//				}
//			}
//		}
//		
//		if (null == presentationKey) {
//			throw new ServletException("No presentationKey");
//		}
//		
//		PersistenceManager pm = PMF.get().getPersistenceManager();
//		
//		Presentation presentation = pm.getObjectById(Presentation.class, presentationKey);
//		
//		if (null == presentation) {
//			throw new ServletException("Invalid presentationKey: " + presentationKey);
//		}
//		
//		for (Slide slide : slides) {
//			presentation.addSlide(slide);
//		}
//		
//		pm.close();
//	}
}
